"""
fonttools
pillow
"""

# from fontTools.ttLib import TTFont
# from PIL import Image, ImageDraw, ImageFont
# import io
# import ddddocr
#
# ocr = ddddocr.DdddOcr(show_ad=False)
# font_path = "./static/fonts/96fc7b50b772f52.woff2"
# font = TTFont(font_path)
# img_font = ImageFont.truetype(font_path, size=20)
#
# font_maps = {
#
# }
#
# for code, name in font.getBestCmap().items():
#     # print(code, chr(code), name)
#     img = Image.new("RGB", (25, 25), (255,255,255))
#
#
#     draw = ImageDraw.Draw(img)
#     draw.text((5, -5), chr(code), (0,0,0), font=img_font)
#
#     img.save(f'./static/fonts/imgs/{name}.png')
#     img_bytes = io.BytesIO()
#     img.save(img_bytes, "PNG")
#     r = ocr.classification(img_bytes.getvalue())
#     # print(code, "识别结果", r)
#     font_maps[code] = r
#
# print(font_maps)

from util.font import get_font_maps
font_maps = get_font_maps("./static/fonts/96fc7b50b772f52.woff2")


def parse_field(value):
    result = ""
    for c in value:
        if ord(c) in font_maps:
            result += font_maps[ord(c)]
        else:
            result += c
    return result




import requests

res = requests.post('https://www.dongchedi.com/motor/pc/sh/sh_sku_list?aid=1839&app_name=auto_web_pc', data={
"sh_city_name":"全国",
"page":2,
"limit":20
})
for item in res.json()['data']['search_sh_sku_info_list']:
    print(item['car_name'], parse_field(item['car_source_city_name']), parse_field(item['official_price']), parse_field(item['sh_price']))